const XLSX = require('xlsx');
const fs = require('fs'); //文件模块
const Path = require('path'); //系统路径模块

class iOSXlsClass {
  constructor(path) {
    this.xlsPath = path;
  }

  getHeaderRow(sheet) {
    const headers = []
    const range = XLSX.utils.decode_range(sheet['!ref'])
    let C
    const R = range.s.r
    /* start in the first row */
    for (C = range.s.c; C <= range.e.c; ++C) { /* walk every column in the range */
      const cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })]
      /* find the cell in the first row */
      let hdr = 'UNKNOWN ' + C // <-- replace with your desired default
      if (cell && cell.t) hdr = XLSX.utils.format_cell(cell)
      headers.push(hdr)
    }
    return headers
  }

  readData() {
    const workbook = XLSX.readFile(this.xlsPath, { type: 'array' })
    const firstSheetName = workbook.SheetNames[0]
    const worksheet = workbook.Sheets[firstSheetName]
    const results = XLSX.utils.sheet_to_json(worksheet)
    //console.log('result', results)
    this.generateI18nFiles(results)
  }

  generateI18nFiles(results) {
    const enData = {};
    const zhData = {};
    const hkData = {};

    const arData = {};
    const csData = {};
    const daData = {};
    const deData = {};
    const elData = {};
    const esData = {};
    const faData = {};
    const fiData = {};
    const frData = {};
    const gaData = {};
    const heData = {};
    const hrData = {};
    const huData = {};
    const itData = {};
    const jaData = {};
    const koData = {};
    const nbData = {};

    const plData = {};
    const ptData = {};
    const roData = {};
    const ruData = {};
    const skData = {};
    const svData = {};
    const thData = {};
    const trData = {};
    const ukData = {};
    const viData = {};

    results.forEach(element => {
      console.log(element.key);
 

      enData[element.key] = String(element.en).replace(/:/,"：").replace(/","/,"，");
      zhData[element.key] = String(element.cn).replace(/:/,"：").replace(/","/,"，");
      hkData[element.key] = String(element.hk).replace(/:/,"：").replace(/","/,"，");

      frData[element.key] = String(element.fr).replace(/:/,"：").replace(/","/,"，");
      itData[element.key] = String(element.it).replace(/:/,"：").replace(/","/,"，");
      esData[element.key] = String(element.es).replace(/:/,"：").replace(/","/,"，");
      ptData[element.key] = String(element.pt).replace(/:/,"：").replace(/","/,"，");
      jaData[element.key] = String(element.ja).replace(/:/,"：").replace(/","/,"，");
      deData[element.key] = String(element.de).replace(/:/,"：").replace(/","/,"，");
      plData[element.key] = String(element.pl).replace(/:/,"：").replace(/","/,"，");
      huData[element.key] = String(element.hu).replace(/:/,"：").replace(/","/,"，");
      ruData[element.key] = String(element.ru).replace(/:/,"：").replace(/","/,"，");
      ukData[element.key] = String(element.uk).replace(/:/,"：").replace(/","/,"，");
      koData[element.key] = String(element.ko).replace(/:/,"：").replace(/","/,"，");
      viData[element.key] = String(element.vi).replace(/:/,"：").replace(/","/,"，");
      thData[element.key] = String(element.th).replace(/:/,"：").replace(/","/,"，");
      hrData[element.key] = String(element.hr).replace(/:/,"：").replace(/","/,"，");
      svData[element.key] = String(element.sv).replace(/:/,"：").replace(/","/,"，");
      nbData[element.key] = String(element.nb).replace(/:/,"：").replace(/","/,"，");
      daData[element.key] = String(element.da).replace(/:/,"：").replace(/","/,"，");
      fiData[element.key] = String(element.fi).replace(/:/,"：").replace(/","/,"，");
      csData[element.key] = String(element.cs).replace(/:/,"：").replace(/","/,"，");
      skData[element.key] = String(element.sk).replace(/:/,"：").replace(/","/,"，");
      gaData[element.key] = String(element.ga).replace(/:/,"：").replace(/","/,"，");
      arData[element.key] = String(element.ar).replace(/:/,"：").replace(/","/,"，");
      heData[element.key] = String(element.he).replace(/:/,"：").replace(/","/,"，");
      trData[element.key] = String(element.tr).replace(/:/,"：").replace(/","/,"，");
      roData[element.key] = String(element.ro).replace(/:/,"：").replace(/","/,"，");
      elData[element.key] = String(element.el).replace(/:/,"：").replace(/","/,"，");
      faData[element.key] = String(element.fa).replace(/:/,"：").replace(/","/,"，");


    });

    this.writeJson('./ios_lang/lproj/en.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(enData)));
    this.writeJson('./ios_lang/lproj/zh-Hans.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(zhData)));
    this.writeJson('./ios_lang/lproj/zh-Hant.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(hkData)));

    this.writeJson('./ios_lang/lproj/fr.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(frData)));
    this.writeJson('./ios_lang/lproj/it.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(itData)));
    this.writeJson('./ios_lang/lproj/es.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(esData)));
    this.writeJson('./ios_lang/lproj/pt.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(ptData)));
    this.writeJson('./ios_lang/lproj/ja.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(jaData)));
    this.writeJson('./ios_lang/lproj/de.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(deData)));
    this.writeJson('./ios_lang/lproj/pl.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(plData)));
    this.writeJson('./ios_lang/lproj/hu.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(huData)));
    this.writeJson('./ios_lang/lproj/ru.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(ruData)));
    this.writeJson('./ios_lang/lproj/uk.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(ukData)));
    this.writeJson('./ios_lang/lproj/ko.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(koData)));
    this.writeJson('./ios_lang/lproj/vi.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(viData)));
    this.writeJson('./ios_lang/lproj/th.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(thData)));
    this.writeJson('./ios_lang/lproj/hr-HR.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(hrData)));
    this.writeJson('./ios_lang/lproj/sv-SE.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(svData)));
    this.writeJson('./ios_lang/lproj/nb-NO.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(nbData)));
    this.writeJson('./ios_lang/lproj/da.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(daData)));
    this.writeJson('./ios_lang/lproj/fi.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(fiData)));
    this.writeJson('./ios_lang/lproj/cs.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(csData)));
    this.writeJson('./ios_lang/lproj/sk-SK.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(skData)));
    this.writeJson('./ios_lang/lproj/ga-IE.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(gaData)));
    this.writeJson('./ios_lang/lproj/ar.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(arData)));
    this.writeJson('./ios_lang/lproj/he.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(heData)));
    this.writeJson('./ios_lang/lproj/fa-IR.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(faData)));
    this.writeJson('./ios_lang/lproj/tr.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(trData)));
    this.writeJson('./ios_lang/lproj/ro.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(roData)));
    this.writeJson('./ios_lang/lproj/el.lproj/Localizable.strings', this.formatLprojJSON(JSON.stringify(elData)));
    //it fr


  }
  formatValueJSON(data){
   return data.replace(/:/,"：").data.replace(/","/,"，");
  }

  formatLprojJSON(data) {

    data = data.replace(/,/g, ';\n').replace(/:/g, ' = ').replace(/{/g, '').replace(/}/g, '') + ";";

    return this.formatMarkJSON(data)
  }
  formatMarkJSON(data) {
    // var repelaseList = ["ios_mark_lang", "ios_mark_unit", "ios_mark_refresh_text", "ios_mark_network_text", "ios_mark_sysnc_data", "ios_mark_login_text", "ios_mark_login_text", "ios_mark_home_health", "ios_mark_heart_electrocardiogram", "ios_mark_sport", "ios_mark_care", "ios_mark_setting","ios_mark_person_info","ios_mark_my_device","ios_mark_dial_market","ios_mark_push_message","ios_mark_health_setting","ios_mark_protection_against_loss", "ios_mark_more_seeting",]
    // for(var i =0 ;i<repelaseList.length;i++){
    //   var r = repelaseList[i] + " = ";
    //   data.replace(r, '// ');
    //   // data.replace(/"ios_mark_lang" = "/g, '// ');
    // }
    return data.replace(/"ios_mark_lang" = "/g, '// ')
    .replace(/"ios_mark_unit" = "/g, '// ')
    .replace(/"ios_mark_refresh_text" = "/g, '// ')
    .replace(/"ios_mark_network_text" = "/g, '// ')
    .replace(/"ios_mark_sysnc_data" = "/g, '// ')
    .replace(/"ios_mark_login_text" = "/g, '// ')
    .replace(/"ios_mark_home_health" = "/g, '// ')
    .replace(/"ios_mark_heart_electrocardiogram" = "/g, '// ')
    .replace(/"ios_mark_sport" = "/g, '// ')
    .replace(/"ios_mark_care" = "/g, '// ')
    .replace(/"ios_mark_setting" = "/g, '// ')
    .replace(/"ios_mark_person_info" = "/g, '// ')
    .replace(/"ios_mark_my_device" = "/g, '// ')
    .replace(/"ios_mark_dial_market" = "/g, '// ')
    .replace(/"ios_mark_push_message" = "/g, '// ')
    .replace(/"ios_mark_health_setting" = "/g, '// ')
    .replace(/"ios_mark_protection_against_loss" = "/g, '// ')
    .replace(/"ios_mark_more_seeting" = "/g, '// ')
    .replace(/"ios_mark_inflatable_blood_pressure_collection" = "/g, '// ')
    
    .replace(/ =  -/g, ': -')
  }

  writeJson(path, content) {
    const targetFile = Path.join(__dirname, path);

    fs.writeFile(targetFile, content, function (err) {
      if (err) {
        return console.error('error:' + err);
      }
      console.log('文件创建成功，地址：' + targetFile);
    });

  }
}

module.exports = iOSXlsClass